/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Business.RegionOperation;

import Business.Region.Country.Country;
import Business.Region.Hospital.Hospital;
import Business.Region.Provincy.Provincy;
import Business.RegionDBUtil.RegionDBUtil;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author hooting
 */
public class RegionOperation {
	public List<Country> getAllCountries(){
		RegionDBUtil db = new RegionDBUtil();
		return db.retrieveAllCountries();
	}

	public boolean updateRegion(String country, String provincy, String hospital){
		RegionDBUtil dbUtil = new RegionDBUtil();
		List<Country> coutryList = dbUtil.retrieveAllCountries();
		Iterator<Country> ite = coutryList.iterator();
		while(ite.hasNext()){
			Country c = ite.next();
			if(country.equals(c.getCountryName())){
				List<Provincy> provincyList = c.getProvincyList();
				Iterator<Provincy> iteProvincy = provincyList.iterator();
				while(iteProvincy.hasNext()){
					Provincy p = iteProvincy.next();
					if(provincy.equals(p.getProvincyName())){
						List<Hospital> hospitalList = p.getHospitalList();
						for(int i = 0; i < hospitalList.size(); i++){
							Hospital h = hospitalList.get(i);
							if(hospital.equals(h.getHospitalName())){
								return false;
							}
						}
						Hospital h = new Hospital(hospital);
						hospitalList.add(h);
						new RegionDBUtil().updateCountry(c);
						return true;
					}
				}
				Provincy p = new Provincy(provincy);
				Hospital h = new Hospital(hospital);
				p.addHospital(h);
				c.addProvincy(p);
				new RegionDBUtil().updateCountry(c);
				return true;
			}
		}
		Country c = new Country(country);
		Provincy p = new Provincy(provincy);
		Hospital h = new Hospital(hospital);
		p.addHospital(h);
		c.addProvincy(p);
		new RegionDBUtil().insertCountry(c);
		return true;
	}

	public boolean deleteRegion(String country, String provincy, String hospital){
		RegionDBUtil db = new RegionDBUtil();
		if(country.equals("others")){
			db.deleteAllCountries();
			return true;
		}
		List<Country> coutryList = db.retrieveAllCountries();
		Iterator<Country> ite = coutryList.iterator();
		while(ite.hasNext()){
			Country c = ite.next();
			if(country.equals(c.getCountryName())){
				if(provincy.equals("others")){
					c.getProvincyList().clear();
					new RegionDBUtil().updateCountry(c);
					return true;
				}
				List<Provincy> provincyList = c.getProvincyList();
				Iterator<Provincy> iteProvincy = provincyList.iterator();
				while(iteProvincy.hasNext()){
					Provincy p = iteProvincy.next();
					if(provincy.equals(p.getProvincyName())){
						if(hospital.equals("others")){
							p.getHospitalList().clear();
							new RegionDBUtil().updateCountry(c);
							return true;
						}
						List<Hospital> hospitalList = p.getHospitalList();
						for(int i = 0; i < hospitalList.size(); i++){
							Hospital h = hospitalList.get(i);
							if(hospital.equals(h.getHospitalName())){
								hospitalList.remove(h);
								new RegionDBUtil().updateCountry(c);
								return true;
							}
						}
						return false;
					}
				}
			}
		}
		return false;
	}
}
